﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace TestePI2
{
    public partial class frmBusca : Form
    {

        public frmBusca()
        {
            
                SqlConnection conn = new SqlConnection("Data Source=EMERSON-PC\\SQLEXPRESS;Initial Catalog=PROJETO_MUSICA;Integrated Security=True");
                conn.Open();             
                SqlCommand cmd = new SqlCommand("DELETE FROM TMP_PEDIDO_ITEM", conn);
                SqlCommand cmd2 = new SqlCommand("DELETE FROM TMP_PEDIDO", conn);
                cmd.ExecuteNonQuery();
                cmd2.ExecuteNonQuery();
                conn.Close();

            
            InitializeComponent();

            cmbTipoBusca.SelectedIndex = 0;

            
        }

        public int IdAlbum { get; set; }

        private void txtBusca_TextChanged(object sender, EventArgs e)
        {
            btnBuscar.Enabled = txtBusca.Text.Trim().Length > 0;
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            dgvMusicas.DataSource = dsPI4.SP_BUSCA_MUSICA;

            dsPI4TableAdapters.SP_BUSCA_ALBUMTableAdapter busca = new dsPI4TableAdapters.SP_BUSCA_ALBUMTableAdapter();
            dgvAlbuns.DataSource = busca.GetData(txtBusca.Text, cmbTipoBusca.SelectedIndex + 1);
        }

        private void dgvAlbuns_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string sCdAlbum = GetRowValue(dgvAlbuns, e.RowIndex, "CD_ALBUM").ToString();

            IdAlbum = Int32.Parse(sCdAlbum);

            lblArtista.Text = String.Format(GetRowValue(dgvAlbuns, e.RowIndex, "Artista").ToString());
            lblAlbum.Text = String.Format(GetRowValue(dgvAlbuns, e.RowIndex, "Album").ToString());
            lblAno.Text = String.Format(GetRowValue(dgvAlbuns, e.RowIndex, "Ano").ToString());
            lblGenero.Text = String.Format(GetRowValue(dgvAlbuns, e.RowIndex, "Genero").ToString());
            lblPreco.Text = String.Format(GetRowValue(dgvAlbuns, e.RowIndex, "Preco").ToString());

            dsPI4TableAdapters.SP_BUSCA_MUSICATableAdapter musicas = new dsPI4TableAdapters.SP_BUSCA_MUSICATableAdapter();
            dgvMusicas.DataSource = musicas.GetData(IdAlbum);

            btnAddCarrinho.Enabled = dgvMusicas.Rows.Count > 0;
        }

        private object GetRowValue(DataGridView gridView, int rowIndex, string columnName)
        {
            if (rowIndex > -1)
            {
                return ((DataRowView)gridView.Rows[rowIndex].DataBoundItem).Row.ItemArray[gridView.Columns[columnName].Index];
            }
            else
            {
                return null;
            }
        }

        private void btnAddCarrinho_Click(object sender, EventArgs e)
        {
            dsPI4TableAdapters.SP_CADASTRO_CARRINHOTableAdapter addCarrinho = new dsPI4TableAdapters.SP_CADASTRO_CARRINHOTableAdapter();
            using (DataTable dt = addCarrinho.GetData(IdAlbum, (int)nudQuantidade.Value))
            {
                if (dt != null && dt.Rows[0]["Valor total"] != null)
                {
                    btnCarrinho.Text = String.Format("&Carrinho: ({0})...", dt.Rows[0]["Valor total"]);
                }
                else
                {
                    btnCarrinho.Text = "&Carrinho...";
                }
                dsPI4TableAdapters.SP_LISTA_CARRINHOTableAdapter listaCarrinho = new dsPI4TableAdapters.SP_LISTA_CARRINHOTableAdapter();
                this.sP_LISTA_CARRINHOTableAdapter.Fill(this.pROJETO_MUSICADataSet.SP_LISTA_CARRINHO);
            }
        }

        private void btnCarrinho_Click(object sender, EventArgs e)
        {
            //using (frmCarrinho frm = new frmCarrinho())
            //{
            //    frm.ShowDialog();

            using(frmCadastro frmCadastro = new frmCadastro())
            {
                frmCadastro.ShowDialog();

                //Após finalizar o carrinho, gravar o conteúdo das tabelas temporárias nas principais
                //E executar a procedure de limpeza
            }
        }

        private void frmBusca_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'pROJETO_MUSICADataSet.SP_LISTA_CARRINHO' table. You can move, or remove it, as needed.
            this.sP_LISTA_CARRINHOTableAdapter.Fill(this.pROJETO_MUSICADataSet.SP_LISTA_CARRINHO);

        }
    }
}
